package com.lhd.web;

import com.lhd.po.Blog;
import com.lhd.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuhandong on 2022/4/20.
 */
@RestController
public class IndexController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;


    @GetMapping("/blog/{id}")
    public Map blog(@PathVariable Long id) {
        Blog blog = blogService.getAndConvert(id);
        blog.setType(typeService.getType(blog.getTypeId()));
        if (blog.getViews() == null){
            blog.setViews(1);
        }else {
            blog.setViews(blog.getViews()+1);
        }
        if (blog.getUser() == null){
            blog.setUser(userService.getUserById(1L));
        }
        blogService.updateBlog(blog.getId(),blog);
        Map<String,Object> map = new HashMap<>();
        map.put("blog", blog);
        map.put("comments", commentService.listCommentByBlogId(blog.getId()));
        map.put("user",blog.getUser());
        return map;
    }

    @GetMapping("/top-blogs/{page}")
    public Map getTopFiveBlog(@PathVariable Integer page){
        Map<String,Object> map = new HashMap<>();
        List<Blog> blogs = blogService.getTopFiveBlog(page);
        for (Blog blog : blogs) {
            blog.setType(typeService.getType(blog.getTypeId()));
        }
        Long count = blogService.countBlog();
        Integer rem = Math.toIntExact(count % 5);
        map.put("page",blogs);
        map.put("curPage",page);
        map.put("preBoolean",page > 0);
        map.put("nexBoolean",page < ((count/5 + (rem == 0 ? 0 : 1))-1));
        return map;
    }

    @GetMapping("/")
    public Map index(@RequestParam(value = "page", required = false, defaultValue = "0") Integer page) {
        // page 页码，一页3篇博客
        List<Blog> blogs = blogService.listBlogByPage(page);
        for (Blog b:blogs){
            if (b.getUserId() == null){
                b.setUserId(1L);
            }
            b.setUser(userService.getUserById(b.getUserId()));
            b.setType(typeService.getType(b.getTypeId()));
        }
        Map<String, Object> map =new HashMap<>();
        map.put("types",typeService.topFive());
        map.put("page",blogs);
        map.put("tags",tagService.topSeven());
        Long count = blogService.countBlog();
        Integer rem = Math.toIntExact(count % 3);
        map.put("blogCount",blogService.countBlog());
        map.put("pageNum",count/3 + (rem == 0 ? 0 : 1));
        map.put("curPage",page);
        map.put("preBoolean",page > 0);
        map.put("nexBoolean",page < ((count/3 + (rem == 0 ? 0 : 1))-1));
        return map;
    }
}
